/*!
 * \file sample_init.c
 *
 * \brief 共通定义
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 * 共通使用的宏和结构体
 *
 * \version 1.0
 * \author jikw
 * \date 2016/2/22
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "frcomm.h"
#include "frmem.h"
#include "frstr.h"
#include "conntrack.h"
#include "module.h"
#include "frversion.h"
#include "config.h"
#include "sample_core.h"
#include "frlog.h"

//框架的上下文引用
void *g_p_apm_context=NULL;

//response中需要保存的数据类型
FR_STATIC char *p_needed_data_type = NULL;

#ifdef __cplusplus
extern "C"
{
#endif

/*!
 * sample初始化
 *
 * \param p_context       (框架的上下文引用)
 * \param p_module_params (模块入力参数)
 * \param p_module_cfg    (模块配置信息出力)
 *
 * \return FR_FAIL   失败\n
 *         FR_SUCC   成功
 *
 * \note
 *
 */
int module_init(void *p_context, const char* p_module_params, L7_module_config *p_module_cfg)
{
    int i_ret = FR_SUCC;
    char *p_pos_start = NULL;
    int i_len = 0;

    if(p_context == NULL || p_module_params == NULL || p_module_cfg == NULL)
        return FR_FAIL;

    if( fr_slab_init( (Frmem_env *)(((ApmContext *)p_context)->p_st_mem_env), MODULE_NAME) == NULL )
        return FR_FAIL;

    //获取要输出数据的类型
    p_pos_start = (char *)strcasestr(p_module_params, "o=");
    if(p_pos_start)
    {
        p_pos_start+=2;
        i_len = strlen(p_pos_start) + 1;
        p_needed_data_type = (char *)frmalloc(i_len);
        if(p_needed_data_type == NULL)
            return FR_FAIL;
        snprintf(p_needed_data_type,i_len,"%s",p_pos_start);
        frtrim(p_needed_data_type);
    }
    g_p_apm_context = p_context;

    frlog_set_hd(((ApmContext *)p_context)->p_st_log_category);
    p_module_cfg->p_l4_proto = MODULE_L4PROTO;
    p_module_cfg->i_l7_proto = MODULE_L7PROTO;
    p_module_cfg->p_module_name = MODULE_NAME;
    p_module_cfg->p_version = MODULE_VERSION " " MODULE_TIME;
    p_module_cfg->l7_detect = sample_detect;
    p_module_cfg->l7_analysis = sample_analysis;
    p_module_cfg->session_destory = sample_session_destroy;

    return FR_SUCC;
}

/*!
 * sample销毁
 *
 * \param void
 *
 * \return void
 *
 * \note
 *
 */
void module_exit(void)
{
    if(p_needed_data_type)
    {
        frfree(p_needed_data_type);
        p_needed_data_type = NULL;
    }
    fr_slab_destroy();

    return;
}

#ifdef __cplusplus
}
#endif